﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinHBM.Core.Bluetooth;

namespace HbmTools.Utils
{

//绑定教师ID
//读教师ID
//读IC卡
//写IC卡
//返回缓存信息（签到数据）
//返回缓存信息（送红花数据）
//返回缓存中红花数据总数
//返回缓存中签到、签退数据总数
//返回电池电量
//设置音量
//检查音频文件
//校准时间
//设置是否限制NFC卡
//设置是否限制签到、签退模式
//擦除整个FLASH


    class SendCMDManager
    {

        private MagicBar bar = null;
        private static SendCMDManager instance = null;
         
        private SendCMDManager()
        {
            bar = BluetoothClientHolder.magicBar;
            bar.SendCMDByte += SendCMDByte;
            bar.ReadCMDByte += ReadCMDByte;
        }

        public static SendCMDManager INSTANCE() {
            if (instance == null) {
                instance = new SendCMDManager();
              
            }
            return instance;
        }

        private void SendCMDByte(byte[] bytes)
        {
            string str = "";
            byte[] arrays = bytes.ToArray();
            for (int i = 0; i < arrays.Length; i++)
            {
                str = str + arrays[i] + " ";
            }

            UtilLog.inputLog(FormMain.textBoxBindData, str);

        }

        private void ReadCMDByte(byte[] bytes)
        {
            string str = "";
            byte[] arrays = bytes.ToArray();
            for(int i = 0;i<arrays.Length;i++)
            {
                str = str + arrays[i] + " ";
            }
            UtilLog.inputLog(FormMain.textBoxBindData, str);
        }

        


        public void sendCMD(string  cmd, TextBoxBindData textBoxBindData)
        {
            switch (cmd)
            {
                case "绑定教师ID":
                    bool bindTeacherIDState=  bar.CMD_BindTeacherId(1024);
                    if (bindTeacherIDState)
                    {
                      UtilLog.inputLog(textBoxBindData, MessageInfo.BindTeacherIDSuccess + 1024);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.BindTeacherIDError);
                    }
                    break;

                case "读教师ID":
                    int teacherID = bar.CMD_ReadTeacherId();
                    UtilLog.inputLog(textBoxBindData, MessageInfo.ReadBindTeacherID + teacherID);
                    break;

                case "写IC卡":
                    bool writeRedCarState = bar.CMD_WriteRedCard(1000);
                    if (writeRedCarState)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.WriteRedCardSuccess + 1000);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.WriteRedCardError);
                    }
                   
                    break;

                case "读IC卡":
                    int readRedCarNum = bar.CMD_ReadRedCard();
                    UtilLog.inputLog(textBoxBindData, MessageInfo.ReadRedCard + readRedCarNum);
                    break;
                    
                case "返回缓存信息(签到数据)":
                      RedFlower flower_1 = bar.CMD_ReadCacheSign();
                      if (flower_1 == null) {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ReadSignCache + "无记录");
                      }else
                     {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ReadSignCache + flower_1.ToString());
                     }
                      break;

                case "返回缓存信息(送红花数据)":
                     RedFlower flower =   bar.CMD_GetRedData();
                    if (flower == null)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ReadRedCache + "无记录");
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ReadRedCache + flower.ToString());
                    }
                       
                     break;
                case "返回缓存中红花数据总数":
                     int cacheFlowerCount =  bar.CMD_GetCacheFlowerCount();
                     UtilLog.inputLog(textBoxBindData, MessageInfo.ReadRedCacheCount + cacheFlowerCount);
                     break;

                case "返回缓存中签到、签退数据总数":
                    int cacheSignCount = bar.CMD_GetCacheSignCount();
                    UtilLog.inputLog(textBoxBindData, MessageInfo.ReadSignCacheCount + cacheSignCount);
                    break;

                case "设置音量":
                    bool settingCoiceState = bar.CMD_SettingVoice(15);
                    if (settingCoiceState)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.SettingVoiceSuccess + 15);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.SettingVoiceError);
                    }
                   
                    break;

                case "检查音频文件":
                    int checkState =  bar.CMD_CheckAudioFile(1000);
                    if (checkState == 1)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.CheckAudioFileNo);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.CheckAudioFileYes);
                    }
                    break;

                case "校准时间":
                    bool checkTimeState = bar.CMD_GetNowTime();
                    if (checkTimeState)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.CheckTimeSuccess);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.CheckTimeError);
                    }
                    break;

                case "返回电池电量":
                    int checkBatteryState = bar.CMD_GetBattery();
                    UtilLog.inputLog(textBoxBindData, MessageInfo.CheckBatteryState+ checkBatteryState);
                    break;


                case "擦除整个FLASH":
                    bool clearFlashState = bar.CMD_ClearFlash();
                    if (clearFlashState)
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ClearFlashSuccess);
                    }
                    else
                    {
                        UtilLog.inputLog(textBoxBindData, MessageInfo.ClearFlashError);
                    }
                    break;

            }

        }
  
    }
}
